package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.GroupMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.GroupMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.GroupMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.GroupMchntInfoBean;
import cc.rengu.igas.bomp.facade.request.GroupMchntListQueryRequest;
import cc.rengu.igas.bomp.facade.response.GroupMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 集团商户列表查询
 * Created by 赵奎伍
 */
public class GroupMchntListQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GroupMchntListQueryRequest groupMchntListQueryRequest = new GroupMchntListQueryRequest();
        ConvertUtil.convertOutput(groupMchntListQueryRequest);
        return groupMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        GroupMchntListQueryRequest requestParams = (GroupMchntListQueryRequest) request;
        GroupMchntListQueryResponse responseParams = new GroupMchntListQueryResponse();
        BeanUtil.beanCopy(requestParams, responseParams);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(responseParams);
        String instId = requestParams.getHeader().getInstId();
        String channelId = requestParams.getHeader().getChanlId();

        //region 查询字段
        String groupMchntNo = requestParams.getGroupMchntNo();
        String groupMchntName = requestParams.getGroupMchntName();
        String signInstId = requestParams.getSignInstId();
        int pageIndex = requestParams.getPageIndex();
        int pageSize = requestParams.getPageSize();
        rglog.debug("查询字段:groupMchntNo<{}>,groupMchntName<{}>,signInstId<{}>,pageIndex<{}>,pageSize<{}>", groupMchntNo, groupMchntName, signInstId, pageIndex, pageSize);
        //endregion
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        GroupMchntInfoMapper groupMchntInfoMapper = new GroupMchntInfoMapperImpl();
        List<GroupMchntInfo> groupMchntInfoList = new ArrayList<>();

        if (-1 == pageIndex || -1 == pageSize) {
            groupMchntInfoList = groupMchntInfoMapper.selectGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId(instId, groupMchntNo, groupMchntName, null);
        } else {
            PageResult pageResult = groupMchntInfoMapper.selectGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId(instId, groupMchntNo, groupMchntName, null, pageInfo);
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条集团商户列表查询!");
                responseParams.setTotalPage(BompAppParamConstant.INT_ZERO);
                responseParams.setTotalRows(BompAppParamConstant.INT_ZERO);
                responseParams.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                responseParams.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

            for (Object object : pageResult.getResult()) {
                groupMchntInfoList.add((GroupMchntInfo) object);
            }
            responseParams.setTotalPage(pageResult.getPageInfo().getTotalPage());
            responseParams.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }


        if (!CollectionUtils.isEmpty(groupMchntInfoList)) {
            /* 按照创建时间降序排序 */
//            groupMchntInfoList = groupMchntInfoList.stream().sorted(Comparator.comparing(GroupMchntInfo::getCreateTime)).collect(Collectors.toList());
//            Collections.reverse(groupMchntInfoList);

            List<GroupMchntInfoBean> groupMchntInfoBeans = new ArrayList<>();
            groupMchntInfoList.forEach(item -> convGroupMchntInfo(item, groupMchntInfoBeans));
            responseParams.setGroupMchntInfoList(groupMchntInfoBeans);
        }

        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GroupMchntListQueryResponse groupMchntListQueryResponse = (GroupMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(groupMchntListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param groupMchntInfo      数据库表结构对象
     * @param groupMchntInfoBeans 接口对象
     */
    private void convGroupMchntInfo(GroupMchntInfo groupMchntInfo, List<GroupMchntInfoBean> groupMchntInfoBeans) {
        if ("01".equals(groupMchntInfo.getGroupMchntStatus())) {
            //只展示正式的数据  00-非正式 01-正式 02-停用 03-已注销
            GroupMchntInfoBean groupMchntInfoBean = new GroupMchntInfoBean();
            BeanUtil.beanCopy(groupMchntInfo, groupMchntInfoBean);
            groupMchntInfoBean.setSignInstId(groupMchntInfo.getManageInstId());//签约机构代码 todo word界面设计图无此字段 待确认
            groupMchntInfoBean.setSignInstName("");//签约机构名称 todo word界面设计图无此字段 待确认
            groupMchntInfoBeans.add(groupMchntInfoBean);
        }
    }
}
